/**
 * 模态框
 */
module.exports = function($scope, $modalInstance, $resource, item){

    $scope.view = {};
    $scope.sel_list = [];

    start();
    init();
    //查询全部景区
    function start(){
        $resource('/api/as/tc/placeview/jlist', {}, {}).save({'type': 'J'}, function (res) {
            if (res.errcode === 0) {
                $scope.place_list = res.data;
            } else {
                alert(res.errmsg);
            }
        });
    }

    //查询已经绑定的景区
    function init(){
        $resource('/api/as/tc/placeview/forOfficeList', {}, {}).save({'binding_place': item.binding_place}, function (res) {
            if (res.errcode === 0) {
                $scope.sel_list = res.data;
            } else {
                alert(res.errmsg);
            }
        });
    }

    //添加
    $scope.add = function () {
        for(var i = 0; i < $scope.sel_list.length; i++){
            if($scope.sel_list[i].id == $scope.view.place.id){
                return;
            }
        }
        $scope.sel_list.push($scope.view.place);
    };

    //移除
    $scope.remove = function (index) {
        $scope.sel_list.splice(index,1);
    };

    //确认
    $scope.ok = function () {
        var code = '';
        for(var i = 0; i < $scope.sel_list.length; i++){
            code += $scope.sel_list[i].code+',';
        }
        $resource('/api/as/sc/office/setOffBindPlace', {}, {}).save({'id': item.id, 'code': code}, function (res) {
            if (res.errcode === 0) {
                $modalInstance.close();
            } else {
                alert(res.errmsg);
            }
        });
    }


    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };


};